Fix issue #20 by specifying explicit target path for depencies install#36
Fix issue #20 by specifying explicit target path for depencies install#36ChrisMzz wants to merge 3 commits into
Conversation
|
Just saw you assigned me to this @stefanhahmann and noticed that your I tested this PR by making sure the installation would work when running Do you have a way of testing this? I'm not much of a Java person so I'm not sure how to compile into a jar, although I'm fairly certain this change I made will not fix the problem if the |
|
@ChrisMzz Thank you very much for your contribution and sorry for not having it seen earlier. I have to admit that I took over the task of maintaining this repo, but I did not actually develop this. This was mainly done by @maarzt My way of testing would be:
We unfortunately do not have an automated test for this. May I ask you to run through this procudure on your device. If this is successful, I would try to run it through on a Windows machine that I have access to (and perhaps also a Mac). If all of this is successful, then the PR is ready to be merged. For the current state of the code, I have already compile the jar: mastodon-blender-view-0.3.5-SNAPSHOT.jar.zip |
|
Thanks for the instructions (good to know in general for Java projects too!), I was able to compile my jar into a String output = runCommandGetOutput( blenderPath.toString(), //
"--background", //
"--python", pythonScript.toAbsolutePath().toString(), //
"--", addonZip.toAbsolutePath().toString() ); //which quite explicitly does the exact same thing as my manual test as I was trying to resolve the issue. I guess it's been a little while. I made sure to check in the Blender installations I selected through the setup, that the For reference:
|
Thanks for testing. May I ask you to modify the PR such that these further changes are included as well? |
|
Oh sorry if it wasn't clear, this isn't a change, it's in L61 in BlenderSetupUtils.java file that is already in the source code. The reason I brought it up is to specify that the installer uses Blender's python installation already, so there's no additional checks needed. If you approve the PR and compile the jar with the code as it is currently it should produce a valid jar that fixes the issue :) |
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #20 where Python package dependencies were being looked up from incorrect locations on Windows (such as AppData/Roaming/Python instead of Blender's Python distribution). The fix explicitly specifies the target installation path for pip to ensure dependencies are installed in Blender's bundled Python environment.
Changes:
- Added explicit
--targetflag to pip install command pointing to Blender's Python site-packages directory - Added
--upgradeflag to ensure packages are updated if already present - Imported
pathlib.Pathfor cross-platform path construction
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
maarzt
left a comment
There was a problem hiding this comment.
Thank you @ChrisMzz for investigating this issue so thoroughly and providing a PR. And thank you @stefanhahmann for reviewing this issue further. Here is a short summary of what I got:
- On Windows under some circumstances the install script installs packages in the wrong directory.
- And adding the
--targetflag to thepip installcommand with a correct "site-packages" folder fixes the problem.
Unfortunately the suggested change breaks the install script on linux (Ubuntu). So we cannot merge this PR as it is. The correct paths on my machines are:
/home/arzt/Applications/blender-5.0.1-linux-x64/5.0/python/lib/python3.11/site-packages
/home/arzt/Applications/blender-5.0.1-linux-x64/5.0/python/bin/python3.11
So the script constructs the wrong path.
With all the provided info. I very much wonder if this is related the "Portable Blender" requirement. Mastodon is - for now - only expected to work with a "Protable Blender" installation. I hope you already double checked this. But just to be sure. I have to ask. Blender comes in two variants for Windows. Do you use the portable version of Blender: Blender Portable (.zip) and not the standard Blender Installer.
See https://www.blender.org/download/:

How this could be fixed:
- Verify if using
Portable Blenderfixes the problem or not. - Let's find out what are the proper install directories in Windows, Mac, Linux. Where blender.exe, python.exe and the
site-packagesfolders. This would be interesting for both the portable and non-portable Blender. @ChrisMzz, @stefanhahmann Could you let me no what the paths are on your machines. - Find a way to construct the correct install path. Maybe use
site.getsitepackages(). - Fix the install_addon.py script. Maybe move the installation entirely inside the Blender plugin.
|
I did indeed use the portable version of Blender. but it returns other things as well, so I need to test this a bit. My bad for including |
Thank you for double checking! To be honest. I'm very surprised that Blender runs in a python installation where
... interesting. Maybe pip uses one of the folders that don't work. Your updated
No worries. To be honest, I'm not sure whether having this |
maarzt
left a comment
There was a problem hiding this comment.
Small fix to have the installation succeed even if a "site-packages" folder is not found.
…site.getsitepackages` output
maarzt
left a comment
There was a problem hiding this comment.
While verifying that we use pip install correctly in the connection with Blender, and especially trying to get an answer to the question: What path to pass to the --target flag. I came across this repo blender-python-example. It suggests a different solution that will likely fix #20. The only thing required is to pass an environment variable PYTHONNOUSERSITE=1 to pip install. That way pip should install the dependencies into the correct folder.
I think I was able to reproduce the problem on my computer. And the fix PYTHONNOUSERSITE=1 worked as expected.
@ChrisMzz Sorry to ask you for another test run. Could you verify that this new solution works on you machine as well. That would be awesome.
Remark: It's worth noting, that Mastodon Blender View, is not a well behaved Blender plugin. As it breaks the Blender Add-on Guidelines. Blender Addon should not call pip at all.
| from pathlib import Path | ||
| import site |
There was a problem hiding this comment.
| from pathlib import Path | |
| import site |
not required anymore
| def get_target_flags(): | ||
| # On Windows pip will sometimes install dependencies to the wrong folder when not setting the target flag. | ||
| # See: https://github.com/mastodon-sc/mastodon-blender-view/pull/36 | ||
| for path in site.getsitepackages(): | ||
| if "site-packages" in path: | ||
| return ["--target", path] | ||
| return [] |
There was a problem hiding this comment.
| def get_target_flags(): | |
| # On Windows pip will sometimes install dependencies to the wrong folder when not setting the target flag. | |
| # See: https://github.com/mastodon-sc/mastodon-blender-view/pull/36 | |
| for path in site.getsitepackages(): | |
| if "site-packages" in path: | |
| return ["--target", path] | |
| return [] |
not required
| target_flags = get_target_flags() | ||
|
|
||
| packages = ['grpcio', 'bidict', 'grpcio-tools', 'pandas'] | ||
| subprocess.check_output([python_path, '-m', 'pip', 'install', *target_flags, *packages]) |
There was a problem hiding this comment.
| target_flags = get_target_flags() | |
| packages = ['grpcio', 'bidict', 'grpcio-tools', 'pandas'] | |
| subprocess.check_output([python_path, '-m', 'pip', 'install', *target_flags, *packages]) | |
| env = dict(os.environ) | |
| env["PYTHONNOUSERSITE"] = "1" | |
| packages = {'grpcio', 'bidict', 'grpcio-tools', 'pandas'} | |
| subprocess.check_call([python_path, '-m', 'pip', 'install', *packages], env=env) |
We don't need to provide a --target flag. Passing a PYTHONNOUSERSITE is apparently the preferred solution, as demonstrated her https://github.com/robertguetzkow/blender-python-examples/blob/92214a9d74eedad8572e890a374d5204623c796a/add_ons/install_dependencies/install_dependencies.py#L114-L117
|
@stefanhahmann Could you try this mastodon-blender-view-0.3.5-SNAPSHOT.jar works for you as well? Steps for testing:
|
|
No worries, I will try to test the new jar this afternoon. Even if it currently breaks the addon guidelines, it's preferable if it installs as intended for all users, independently of their OS. If the jar works for both me and Stefan should I then commit the changes you suggested? |
I did a test with mastodon-blender-view-0.3.5-SNAPSHOT.jar on MacOS. The test failed:
However, this may not be related to this PR and may have failed already with Will do a test on Windows as well. |
I also tested on Windows. My results: When I first tried to install the addon, it ran more than two minutes. Then I cancelled the dialog. After I went to the dialog again, it said "Addon installed". The test said "Addon successfully tested". However, when I tried to open the interactive Blender View the following happened:
I used BlenderPortable 5 for the test. |
|
@stefanhahmann Oh no, that's not good. I need to sit down with you to get an idea what is going on. Unfortunately I'm busy with a lot of other projects. |
Just wanted to report that I tested with |


See my comment on #20.
It seems that on Windows, for some reason Python looks at
site-packagesfrom distributions elsewhere in the computer, notably inAppData/Roaming/Python.I think this is fixed by specifying explicitly the target path for the dependencies to be installed in:
Since this path was implicit in situations where this problem doesn't occur, this change should be inconsequential to those situations.
From what I've tested on my end this works in both the situation with the issue and the situation without the issue.